var count=0;

$(document).ready(function(){
	var num=0;
	$.ajax({
		type:"post",
		url:"talkingRoom",
		data:{
			operate:2,
			name:0,
			content:0,
			sex:0,
			row:0,
		},
		success:function(res){
			num=res.count;
			console.log(num);
			
			for(var i=1;i<=num;i++){
				$.ajax({
					type:"post",
					url:"talkingRoom",
					data:{
						operate:3,
						name:0,
						content:0,
						sex:0,
						row:i,
					},
					success:function(res){
						insert(res.name,res.content,res.sex,2);
					}
				});
			}
			
			
			
			
			
		}
	});
	
	
	
	
	
	
	
	
});

$("#talking_submit").click(function(){
	
	var name=$("#nameInput").val();
	var content=$("#contentInput").val();
	var sex=GetRadioValue("sex");
	
	if(name==null||name==""||content==null||content==""||sex==null||sex==""){
		alert("please finish your talking-content!");
	}else insert(name,content,sex,1);	
});




function insert(name,content,sex,operate){
	if(sex==1){
		$.ajax({
			type:"post",
			url:"talkingRoom",
			data:{
				operate:operate,
				name:name,
				content:content,
				sex:sex,
				row:0,
			},
			success:function(res){
				count=res.count;
			}
		});
		
		
		$.ajax({
			type:"post",
			url:"components/menTalking.html",
			data:{},
			success:function(res){
				$("#talking-content").append($(res));
				$("#menName").html(name);
				$("#menContent").html(content);
				$("#menName").attr("id",count);
				$("#menContent").attr("id",count);
			}
		});
	}else if(sex==2){
		
		$.ajax({
			type:"post",
			url:"talkingRoom",
			data:{
				operate:operate,
				name:name,
				content:content,
				sex:sex,
				row:0,
			},
			success:function(res){
				count=res.count;
			}
		});
		
		$.ajax({
			type:"post",
			url:"components/womenTalking.html",
			data:{},
			success:function(res){
				$("#talking-content").append($(res));
				$("#womenName").html(name);
				$("#womenContent").html(content);
				$("#womenName").attr("id",count);
				$("#womenContent").attr("id",count);
			}
		});
	}
}


function GetRadioValue(RadioName){
    var obj;   
    obj=document.getElementsByName(RadioName);
    if(obj!=null){
        var i;
        for(i=0;i<obj.length;i++){
            if(obj[i].checked){
                return obj[i].value;           
            }
        }
    }else return null;
}



